import common.CommonDemo;
import org.checkerframework.checker.units.qual.C;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.PointerInput;
import org.openqa.selenium.interactions.Sequence;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.time.Duration;
import java.util.Collections;

/**
 * pen的三个操作状态：
 * 0：等价于鼠标左键点击
 * 2：等价于鼠标右键点击
 * 5：暂不支持
 */
public class PenDemo {

    private CommonDemo commonDemo;
    private WebDriver driver;

    @BeforeTest
    public void setUp(){
        try {
            commonDemo = new CommonDemo();
            driver = commonDemo.getChromeDriver();
            String url = commonDemo.getUserDir("src/main/resources/actionPen.html");
            driver.manage().window().maximize();
            driver.get(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * PointerInput.Kind.PEN的使用：移动到指定元素，显示文本内容
     */
    @Test
    public void usePen(){
        WebElement element = driver.findElement(By.id("pointerArea"));
        new Actions(driver)
                .setActivePointer(PointerInput.Kind.PEN, "default pen")
                .pause(Duration.ofSeconds(2))
                .moveToElement(element)
                .clickAndHold()
                .moveByOffset(2,2)
                .release()
                .perform();
    }

    @Test
    public void setPointerEventProperties(){
//        driver.get("https://selenium.dev/selenium/web/pointerActionsPage.html");
        try {
            Thread.sleep(2000);
            WebElement pointerArea = driver.findElement(By.id("pointerArea"));
            PointerInput pen = new PointerInput(PointerInput.Kind.PEN, "default pen");
            PointerInput.PointerEventProperties eventProperties = PointerInput.eventProperties()
                    .setTiltX(-72)
                    .setTiltY(9)
                    .setTwist(86);
            PointerInput.Origin origin = PointerInput.Origin.fromElement(pointerArea);

            Sequence actionListPen = new Sequence(pen, 0)
                    .addAction(pen.createPointerMove(Duration.ZERO, origin, 0, 0))
                    .addAction(pen.createPointerDown(0))
                    .addAction(pen.createPointerMove(Duration.ZERO, origin, 2, 2, eventProperties))
                    .addAction(pen.createPointerUp(0));

            ((RemoteWebDriver) driver).perform(Collections.singletonList(actionListPen));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @AfterTest
    public void tearDown(){
        try {
            Thread.sleep(2000);
            driver.quit();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
